package oa.core;

import redis.clients.jedis.Jedis;

import java.io.IOException;

import org.apache.log4j.Logger;

import oa.test.Employee;


/**
 * Created by wuwenhao on 14/10/27.
 */
public class MyRedis {



    //notice:the serializable object should use the restore function, or the return value will not the origin value
    public void setObject(String key, Object value){
        Jedis jedis = MyRedisPool.getInstance();
        boolean broken = false;
        try {
            jedis.set(key.getBytes(),MySerializable.serializable(value));
        } catch (Exception e) {
            broken = true;
            e.printStackTrace();
        }finally {
           MyRedisPool.releaseSource(jedis,broken);
        }
    }

    public byte[] getObject(byte[] bytes){
        Jedis jedis = MyRedisPool.getInstance();
        boolean broken = false;
        byte b[] = null;
        try {
             b = jedis.get(bytes);
        } catch (Exception e) {
            broken = true;
            e.printStackTrace();
        }finally {
            MyRedisPool.releaseSource(jedis,broken);
        }
        return b;
    }

    public void setStr(String key, String value){
        Jedis jedis = MyRedisPool.getInstance();
        boolean broken = false;
        try {
            jedis.set(key, value);
        } catch (Exception e) {
            broken = true;
            e.printStackTrace();
        }finally {
            MyRedisPool.releaseSource(jedis,broken);
        }
    }

    public String getStr(String key){
        Jedis jedis = MyRedisPool.getInstance();
        boolean broken = false;
        String value = null;
        try {
            value = jedis.get(key);
        }catch (Exception e){
            broken = true;
            e.printStackTrace();
        }finally {
            MyRedisPool.releaseSource(jedis, broken);
        }
        return value;
    }

    public boolean existsKey(String key){
        Jedis jedis = MyRedisPool.getInstance();
        boolean existFlag = false;
        boolean broken = false;
        try {
            if(jedis.exists(key)){
                existFlag = true;
            }
        }catch (Exception e){
            broken = true;
            e.printStackTrace();;
        }finally {
            MyRedisPool.releaseSource(jedis, broken);
            return existFlag;
        }
    }

    public boolean existsKeyByte(byte[] bytes){
        Jedis jedis = MyRedisPool.getInstance();
        boolean existFlag = false;
        boolean broken = false;
        try {
            if(jedis.exists(bytes)){
                existFlag = true;
            }
        }catch (Exception e){
            broken = true;
            e.printStackTrace();;
        }finally {
            MyRedisPool.releaseSource(jedis, broken);
            return existFlag;
        }
    }
    
    public static void main(String[] args) {
    	Logger logger = Logger.getLogger("Test");
    	logger.info("hello log4j");
    	Employee emp = new Employee();
    	emp.setAge(13);
    	emp.setId(13);
    	emp.setName("xiaohaobi");
    	//setObject("Test1", emp);
    	//setObject("Test2", emp);
    	//setObject("Test3", emp);
        Jedis jedis = new Jedis("192.168.1.109", 6379); //MyRedisPool.getInstance();
        jedis.auth("luows");
        String test = jedis.get("Test1");
        
        System.out.println("6.1: " + test);
        byte[] test1 = jedis.get("Test1".getBytes());
        System.out.println("6.2: " + test1);
        try {
			Employee emp1 = (Employee) MySerializable.unSerializable(test1);
			System.out.println("6: " + emp1.getName() + "=" + emp1.getAge() + "=" + emp1.getId());
		} catch (IOException e) {
			e.printStackTrace();
		}
    	
    }
}
